/*
Defined in parameter.js
UInt="UInt";
SInt="SInt";
Enum="Enum List";
String="String";
DosName="DosName";
Fixed ="Fixed";
BitMask="BitMask";
Hex="Hex";				
EnumHex="Enum List Hex";	
*/

/*
 * a helper function to display parameter array content
 */
function displayCapabilities(a)
{

	for(i=1 ; i< a.length ; i++)
	{

				printf(i,") ",a[i].param, " : ", a[i].desc);
				printf(a[i].type, a[i].def, a[i].min, a[i].max);
//				printf(a[i].param, ",", a[i].desc, ",", a[i].min, ", ", a[i].max );
				printf('')
	}



}

function paramNotThere(param , theObj)
{

	for( a in theObj){
		if( param == a)
			return 0;
	}

	return 1;

}

/* Print a series of if..end statements for each parameter tag.  This routine is used
 * to separate the Enum List types from the UInt or String data types
 * the Enum List types correspond to GetCapabilitiesCount and GetCapabilitiesListItem
 * an additional type was added, 'Enum List Hex' to distinguish an Enum List that
 * was in hexadecimal, there are a couple i.e. "flty'.

 * PARAMETERS
 * a		: the parameter array for either DC220, DC260, LEO
 * arguments[] : the tag type(s) to select on(optional)
 * if there are no extra arguments, all parameter tags are printed
 * in the if ... end conditionals
 */
 function loopCapability(a)
{
	var tagType=new Array();
	var match=1;			// default: all cases 
	var check=0;			// default: don't select by type
	var theType;
	

	// check if user has added some tag type parameters to select from
	// place these extra tags into the tagType array
	if (loopCapability.length > 1){
		check=1;
		
		for(i =1 ; i<loopCapability.length ; i++){
			tagType[i-1] = loopCapability.arguments[i];
			}
	}
	
		
// print as unsigned int for most parameters (Enum, UInt)
  printf('theType=','"UInt"');
  for( i=1; i<a.length ; i++)
 {

 	
 	// skip this tag? 
 	// if this tag is one that the user requested
 	// print out the 'if count' statements
 	if( check==1){
			match=0;
		for( twiddle in tagType ){	
			if( a[i].type == tagType[twiddle] )
			match = 1; 
			}					
		}
 	 
 	 
 	// prefer a continue here, but Netscape 3.0 croaks	
 	if(match==1) 		
 	{			
 			printf('if count == ' , i  );
 			printf('param= ', a[i].param  );

			// NOTE:  there is no switch in Netscape 3! (but is in Netcape 4)	
				if( a[i].type == "Enum List" ){
					theType='"Enum List"';		
					printf('theType=',theType);
				}
				else if (a[i].type== "Enum List Hex" ){
					theType='"Enum List Hex"';
					printf('theType=',theType);
				}
				else if ((a[i].type== "Hex" ) || (a[i].type== "BitMask" ) )
				{
					theType='"Hex"';
					printf('theType=',theType);
				}
				else if ( a[i].type== "SInt"  ){
					theType='"SInt"';
					printf('theType=',theType);
				}
				else{
					theType='"String"';
					printf('theType=',theType);
				}
	

					printf('end ');
	}// end of match==1 if
	
	
 }// end of this for loop

}


/* LOOPs through all the parameters and determines if the display should
 * be a String, UInt, SInt or Hex
 *
 */
function loopDisplayLineType(a)
{
		
	// print as unsigned int for most parameters (Enum, UInt)
	printf('theType=','"UInt"');
 for( i=1; i<a.length ; i++)
 {
 
 	var theType;
 	 
 	printf(' ');				
 	printf('if count == ' , i  );
 	printf('param= ', a[i].param  );

	// print types other than UInt (NOTE: no switch before Netscape 4.0)		
	if( (a[i].type=="Enum List Hex") || (a[i].type=="Hex") || (a[i].type=="BitMask") || (a[i].type=="Fixed") )
		printf('theType="Hex"');
	else if ((a[i].type=="String") || (a[i].type=="DosName") || (a[i].type=="PName"))
		printf('theType="String"');		
	else if ( a[i].type=="SInt")
		printf('theType="SInt"');

	printf('end ');

 }

}




function GetCapabilityType(a)
{
	flashpoint();


	printf('# API: GetCapabilityType(PName,capability-type )');
	printf('# return one of 3 values ');
	printf('# 1 = name-value list capability type ');
	printf('# 2 = min-max capability type ');
	printf('# 3 = simple value type ');
	printf('# OUTPUT FORMAT ');
	printf('# PName [status]  parameter tag capability-type ');
	printf('# i.e. bsiz   [0]        1           enum type ');


  writeMenu('"GetCapabilityType"','"CMD"','"GetCapabilityType()"');
  writeVariables();
  
  printf('<br>','#**** OPEN FILE ****');  
  printf('outFile="/System/ZCType.txt"');
  writeFileOpen();

	printf('WriteLine(fd," API: GetCapabilityType(PName,capability-type )")');
	printf('WriteLine(fd," return one of 3 values ")');
	printf('WriteLine(fd," 1 = name-value list capability type ")');
	printf('WriteLine(fd," 2 = min-max capability type") ');
	printf('WriteLine(fd," 3 = simple value type ")');
	printf('WriteLine(fd," OUTPUT FORMAT ")');
	printf('WriteLine(fd," PName [status] parameter tag capability-type" )');
	printf('WriteLine(fd," i.e. bsiz   [0]        1           enum type" )');
	printf('WriteLine(fd," ")');

 	printf(' ');
 	printf('count=0 ');
	printf(' ' );
 	printf('LOOP: <br>');
 	printf(' count=count+1 ');
 	printf('theType=" "');
	printf('  ' );

// every value returned is UInt, so simply print out those damn if....ends
	for( i=1; i<a.length ; i++)
	{
  	printf(' ');				
 	printf('if count == ' , i  );
 	printf('param= ', a[i].param  );
	if( a[i].fposComm == "GetProductInfo" ) printf('theType="GetProductInfo"');
	printf(' end ');
	}

	printf("");
	printf(" uvar=0");
	printf(' status=GetCapabilityType(param,uvar) ');
	printf(' if status==0')
	printf(' DisplayLine(count,") ", param," [",status,"] ",uvar) ');
	printf(' Write(fd, count,") ", param," [",status,"] ") ');
	
	printf(' if uvar == 1 ')
	printf(' WriteLine(fd,uvar,"    enum type ")');
	printf(' end ');
	printf(' if uvar == 2 ')
	printf(' WriteLine(fd,uvar,"    min-max type ")');
	printf(' end ');
	printf(' if uvar == 3 ')
	printf(' WriteLine(fd,uvar, "    simple value type ")');
	printf(' end ');		
	printf(' end');
	
	printf(' ');
	printf(' if status!=0')
	printf(' DisplayLine(count,") ", param," [",status,"] "," err? ",theType) ');
	printf(' WriteLine(fd, count,") ", param," [",status,"] "," err? ",theType) ');
    printf(' end');
	printf(' ');


	printf('Wait(100) ');
	printf('if count <' , a.length -1);
	printf('goto LOOP ');	
	printf('end ');

	writeFileClose();
	printf('exitscript');
}

function GetCapabilitiesRange(a)
{
	flashpoint();

	printf('# API: GetCapabilityRange(PName,min-value, max-value, default-value )');
	printf('# OUTPUT FORMAT ');
	printf('#  PName [status] default (min,max) ');
	printf('# i.e. actc[0]  600 (5,65535)');
	printf('# ');

	writeMenu('"GetCapabilitiesRange"','"CMD"','"GetCapabilitiesRange()"');
	writeVariables();

	printf('<br>','#**** OPEN FILE ****');
  	printf('outFile="/System/ZCRng.txt"');
	writeFileOpen();

	printf('WriteLine(fd," API: GetCapabilityRange(PName,min-value, max-value, default-value ) ")');
	printf('WriteLine(fd," OUTPUT FORMAT ")');
	printf('WriteLine(fd,"  PName [status] default (min,max) ")');
	printf('WriteLine(fd," i.e. actc[0]  600 (5,65535)")');
	printf('WriteLine(fd," ")');

 	printf(' ');
 	printf('count=0 ');
	printf('<br>  ' );
 	printf('LOOP: <br>');
 	printf(' count=count+1 ');
	printf('  ' );
	
	loopDisplayLineType(a);

	printf(' min=0' );
	printf(' max=0' );
	printf(' default=0' );
	printf(' umin=0' );
	printf(' umax=0' );
	printf(' udefault=0' );

	printf(' if theType == "UInt" ')
	printf(' status=GetCapabilitiesRange(param,umin,umax,udefault) ');
	printf(' DisplayLine(count,")" ,param," [",status,"] ",udefault," (",umin,",",umax,")" ) ');
	printf(' WriteLine(fd,count,")" ,param," [",status,"] ",udefault," (",umin,",",umax,")" ) ');
	printf(' end ');

 	printf(' if theType == "SInt" ')
 	printf(' status=GetCapabilitiesRange(param,min,max,default) ');
	printf(' DisplayLine(count,")" ,param," [",status,"] ",default," (",min,",",max,")" ) ');
	printf(' WriteLine(fd,count,")" ,param," [",status,"] ",default," (",min,",",max,")" ) ');
 	printf(' end ');
	
	
	printf(' if theType == "Hex" ')
	printf(' status=GetCapabilitiesRange(param,umin,umax,udefault) ');	
	printf(' bmin=umin');
	printf(' bmax=umax');
	printf(' bdefault=udefault ');
	printf(' DisplayLine(count,")" ,param," [",status,"] ",bdefault," (",bmin,",",bmax,")" ) ');
	printf(' WriteLine(fd,count,")" ,param," [",status,"] ",bdefault," (",bmin,",",bmax,")" ) ');
	printf(' end');
	
	printf(' Wait(100) ');
	printf('if count < ',a.length-1 );
	printf(' goto LOOP ');
	printf(' end ');


	writeFileClose();
	printf('exitscript');

}



function GetCapabilitiesCount(a)
{
	flashpoint();

	printf('# API: GetCapabilitiesCount(PName, list-count, default )');
	printf('# OUTPUT FORMAT ');
	printf('# PName[status]  list-count  default ');
	printf('# i.e. bcpn[0] 4 2');
	printf('# ');



	writeMenu('"GetCapabilitiesCount"' , '"CMD"','"GetCapabilitiesCount()"');
	writeVariables();
	printf('<br>','#**** OPEN FILE ****');
  	printf('outFile="/System/ZCnt.txt"');
	writeFileOpen();

	printf('WriteLine(fd," API: GetCapabilitiesCount(PName, list-count, default )") ');
	printf('WriteLine(fd," OUTPUT FORMAT ") ');
	printf('WriteLine(fd,"  PName[status]  list-count  default ") ');
	printf('WriteLine(fd," i.e. bcpn[0] 4 2") ');
	printf('WriteLine(fd," ") ');




// write out series of IFs 

 	printf(' ');
 	printf('count=0 ');
	printf('<br>  ' );
 	printf('LOOP: <br>');
 	printf(' count=count+1 ');
	printf(' ' );

	// just select on "Enum List" and "Enum List Hex" types
	loopCapability(a,"Enum List","Enum List Hex");

	printf('ucount=0');
	printf('udefault=0');

	printf('# ucount= number of items ');
	printf('# udefault = actual default value (IT IS NOT! an index into the array!)');
	printf(' if theType == "Enum List" ');
	printf('status=GetCapabilitiesCount(param,ucount,udefault) ');
	printf('DisplayLine(count,") ",param,"[",status,"] ",ucount," ",udefault)');	
	printf('WriteLine(fd,count,") ",param,"[",status,"] ",ucount," ",udefault)');	
	printf(' end ');

	printf('# probably the "flty" parameter type ');
	printf(' if theType == "Enum List Hex" ');
	printf('status=GetCapabilitiesCount(param,ucount,udefault) ');
	printf('bdefault = udefault ');
	printf('DisplayLine(count,") ",param,"[",status,"] ",ucount," ",bdefault)');	
	printf('WriteLine(fd,count,") ",param,"[",status,"] ",ucount," ",bdefault)');	
	printf(' end ');

	printf('Wait(100)');
	printf('if count < ',a.length-1 );
		printf('goto LOOP');
	printf('end');



	writeFileClose();
	printf('exitscript');

}


function GetCapabilitiesListItem(a)
{
	flashpoint();

	printf('# API: GetCapabilitiesListItem(PName,list-index, item-desc, item-value )');
	printf('# OUTPUT FORMAT ');
	printf('# PName [status]  (index) desc value ');
	printf('# i.e. ');
	printf('# bcpn[0] (0) Super Fine 5');
	printf('# bcpn[0] (1) Fine 4');
	printf('# bcpn[0] (2) Standard 3');
	printf('# bcpn[0] (3) Economy 2');
	printf('# ');


	writeMenu( '"GetCapabilitiesListItem"', '"CMD"' ,'"GetCapabilitiesListItem()"' );
	writeVariables();
	
	printf('<br>','#**** OPEN FILE ****');
	printf('outFile="/System/ZCLItem.txt"');
	writeFileOpen();

	printf('WriteLine(fd," API: GetCapabilitiesListItem(PName,list-index, item-desc, item-value)" )');
	printf('WriteLine(fd," OUTPUT FORMAT ")');
	printf('WriteLine(fd," PName [status]  (index) desc value ")');
	printf('WriteLine(fd," i.e. ")');
	printf('WriteLine(fd," bcpn[0] (0) Super Fine 5")');
	printf('WriteLine(fd," bcpn[0] (1) Fine 4")');
	printf('WriteLine(fd," bcpn[0] (2) Standard 3")');
	printf('WriteLine(fd," bcpn[0] (3) Economy 2")');
	printf('WriteLine(fd," ")');


 	printf(' ');
 	printf('count=0 ');
	printf('  ' );
 	printf('LOOP: <br>');
 	printf(' count=count+1 ');
	printf('  ' );
	
	loopCapability(a,"Enum List","Enum List Hex");

	printf('# PROCESS Enum List type ');
	printf(' uvar=0');
	printf(' ucount=0');
	printf(' uIndex=0');

	printf(' status=GetCapabilitiesCount(param,ucount,uvar)');
	printf(' icount = ucount ');
	printf(' icount = icount - 1');

	printf('');	
	
	printf(' if theType != "UInt" ')
	printf(' WriteLine(fd," " ) ');
	printf(' end ' );
	printf('ANOTHER:');
	printf(' status=GetCapabilitiesListItem(param,uIndex,sDesc,uValue) ');

	printf(' if theType == "Enum List"');
	printf(' DisplayLine(count,")",param, "[",status,"] ","(",uIndex,")"," ",sDesc," ",uValue) ');
	printf(' WriteLine(fd,count,")",param, "[",status,"] ","(",uIndex,")"," ",sDesc," ",uValue) ');
	printf(' end');

	printf(' # probably "flty" ');
	printf(' if theType == "Enum List Hex"');
	printf(' bvar= uValue');
	printf(' DisplayLine(count,")",param, "[",status,"] ","(",uIndex,")"," ",sDesc," ",bvar) ');
	printf(' WriteLine(fd,count,")",param, "[",status,"] ","(",uIndex,")"," ",sDesc," ",bvar) ');
	printf(' end');

	printf(' uValue=0 ');
	printf(' sDesc=" "');

	printf(' if uIndex < icount ');
	printf(' uIndex = uIndex + 1');
	printf(' goto ANOTHER');
	printf(' end');

	printf(' Wait(100)');
	printf(' if count < ',a.length-1 );
	printf(' goto LOOP ');
	printf(' end ');

	writeFileClose();
	printf('exitscript');
}


function GetCapabilitiesValue(a)
{
	flashpoint();

	printf('# API: GetCapabilitiesValue(PName, value) ');
	printf('# OUTPUT FORMAT');
	printf('# PName [status] value ');
	printf('# "camf" and "cmne" return status 0');
	printf('# for most others, status 5 is returned');
	printf(' ');

	writeMenu('"GetCapabilitiesValue"', '"CMD"','"GetCapabilitiesValue()"');
	writeVariables();
	printf('<br>','#**** OPEN FILE ****');
  	printf('outFile="/System/ZCVal.txt"');
	writeFileOpen();

	printf('WriteLine(fd,"  ")');
	printf('WriteLine(fd," API: GetCapabilitiesValue(PName, value) ")');
	printf('WriteLine(fd,"  OUTPUT FORMAT")');
	printf('WriteLine(fd,"  PName [status] value ")');
	printf('WriteLine(fd,"  camf and cmne return status 0")');
	printf('WriteLine(fd,"  for most others, status 5 is returned")');
	printf('WriteLine(fd,"  ")');


// write out series of IFs 

 	printf(' ');
 	printf('count=0 ');
 	printf('LOOP: <br>');
 	printf(' count=count+1 ');
	printf('  ' );

	loopCapability(a);

	printf(' status=GetCapabilitiesValue(param,svar) ');
	printf(' DisplayLine(count,")",param,"[",status,"] ",svar) ');
	printf(' WriteLine(fd,count,")",param,"[",status,"] ",svar) ');
	printf(' svar="" ');
	printf(' Wait(100) ');
	printf(' if count < ', a.length-1);
	printf(' goto LOOP ');
	printf(' end ');

	writeFileClose();
	printf('exitscript');

}
